package dabs.engine.plant;

import dabs.engine.control.ComponentCommand;
import dabs.engine.environment.AcceptsModifier;
import dabs.engine.environment.Modifier;
import dabs.engine.environment.TemporaryDisable;
import dabs.engine.messaging.Notification;
import dabs.engine.messaging.NotificationHandler;

public class Valve extends ComponentPart implements AcceptsModifier{
	private static final long serialVersionUID = 1918207822741948764L;
	
	private boolean isOpen;
	
	public Valve(NotificationHandler mailBox, String name, boolean openState) {
		super(mailBox, name);
		isOpen = openState;
	}

	public boolean getIsOpen() { return isOpen; }
	
	public void setIsOpen(boolean isOpen){ 
		if (isDeactivated() == false){ 
			this.isOpen = isOpen; 
			if (isOpen = true){
				passMessage(new Notification(getName(), "Opened."));
			}
			else{
				passMessage(new Notification(getName(), "Closed."));
			}
		}
		else { 
			super.passMessage(new Notification(super.getName(), 
					"Valve is blocked."));
		}
	}
	
	@Override
	protected void inactivatedModifier(Modifier modifier) {
		super.inactivatedModifier(modifier);
		if (modifier instanceof TemporaryDisable) {
			passMessage(new Notification(getName(), "is now active again"));
		}
	}
	
	@Override
	public void tick() {
		getNewHydroState().sendToNext(20, true, getOutputs());
		getNewHydroState().sendToNext(20, false, getOutputs());
		super.tick();
	}
	
	@Override
	public void runCommand(ComponentCommand command) {
		assert(command.getDestination().equals(getName()));
		if (command.getArguments().get(0).equals("open")){
			setIsOpen(true);
		}
		if (command.getArguments().get(0).equals("close")){
			setIsOpen(false);
		}
		else{
			passMessage(new Notification(getName(), "Command must be open or close."));
		}
	}
	
	
	
}
